package com.basswold.nasaesa.client;

import java.util.ArrayList;

public class ModuleList implements Cloneable{

	public ModuleList(){
		counter = new int[10];
		moduleList = new ArrayList<Module>();
	}

	public boolean add(int x, int y, int id, String cond, String orient){
		boolean good = true;
		for(int i = 0; i < moduleList.size(); i ++){
			Module m = moduleList.get(i);
			if (id == m.getId() || ( x == moduleList.get(i).getCurrX() && y == moduleList.get(i).getCurrY())){
				good = false;
			}
		}
		if (good == true){

			moduleList.add(new Module(x,y,id, cond, orient));
		}
		return good;
	}

	public void add(Module m){
		moduleList.add(m);
	}
	
	public void incVal(boolean up, int index){
		int i = -1;
		if (up == true){
			i = 1;
		}
		counter[index] += i;
	}

	public String check_all(){
		String returner = "";
		for(int i = 0; i < moduleList.size(); i++){
			returner += moduleList.get(i).getId() + ", ";
		}
		return returner;
	}

	public Module remove(int id){
		int index = -1;
		for(int i =0; i < moduleList.size(); i ++){
			Module m = moduleList.get(i);
			if (id == m.getId()){
				index = i;
			}
		}
		return moduleList.remove(index);
	}
	
	/**
	 * Remove and return the first module in the list that matches a 
	 * specified module type.
	 * @param moduleType The type of module to search for.
	 * @return A module of the specified type if found, null otherwise.
	 */
	public Module removeByType(String moduleType) {
		Module module = null;
		
		// Loop through list of modules
		for (int i = 0; i < moduleList.size(); i++) {
			// Remove matching module if one exists
			if (moduleList.get(i).getType().equals(moduleType)) {
				module = moduleList.remove(i);
				break;
			}
		}
		
		// Returns module if found, null if not
		return module;
	}

	public int count(){
		return moduleList.size();
	}

	public Module getat(int index){
		return moduleList.get(index);
	}

	public Module rm(int index){
		return moduleList.remove(index);
	}

	public boolean minAvail(){
		return ((counter[0] >= 3) &&
				(counter[1] >= 1) &&
				(counter[2] >= 1) &&
				(counter[3] >= 1) &&
				(counter[5] >= 1) &&
				(counter[6] >= 1) &&
				(counter[7] >= 1) &&
				(counter[8] >= 1));
	}

	public String storageengine(){
		String modlist = "{";
		for(int i = 0; i < moduleList.size(); i++){
			modlist += moduleList.get(i).storageString();
		}
		modlist += "}";
		return modlist;
	}
	
	public String storageConfig(){
		String modlist = "{";
		for(int i = 0; i < moduleList.size(); i++){
			modlist += moduleList.get(i).getIDXY();
		}
		modlist += "}";
		return modlist;
	}

	public void reverseStorageConfig(String entireConfigList){
		entireConfigList = entireConfigList.substring(1, entireConfigList.length()-1);
		while(entireConfigList.length() > 0){
			int backBracket = entireConfigList.indexOf('}');
			String modText = entireConfigList.substring(1,backBracket);
			entireConfigList = entireConfigList.substring(backBracket+1);
			int afterid = modText.indexOf(':');
			int afterx = modText.indexOf(':', afterid+1);
			int aftery = modText.length();
			int id = Integer.parseInt(modText.substring(0,afterid));
			int x = Integer.parseInt(modText.substring(afterid+1,afterx));
			int y = Integer.parseInt(modText.substring(afterx+1,aftery));
			// here we alter the given id to match the x,y locations
			for (int i = 0; i < moduleList.size() ; i++){
				Module m = moduleList.get(i);
				if (m.getId() == id){
					m.setCurrX(x);
					m.setCurrY(y);
					break;
				}
			}
		}
		
	}
	
	public void reverseStorage(String entireModList){
		entireModList = entireModList.substring(1, entireModList.length()-1);
		while(entireModList.length() > 0){
			int backBracket = entireModList.indexOf('}');
			String modText = entireModList.substring(1,backBracket);
			entireModList = entireModList.substring(backBracket+1);
			int afterid = modText.indexOf(':');
			int afterx = modText.indexOf(':', afterid+1);
			int aftery = modText.indexOf(':', afterx+1);
			int aftercond = modText.indexOf(':',aftery+1);
			int id = Integer.parseInt(modText.substring(0,afterid));
			int x = Integer.parseInt(modText.substring(afterid+1,afterx));
			int y = Integer.parseInt(modText.substring(afterx+1,aftery));
			String cond = modText.substring(aftery+1, aftercond);
			String orient = modText.substring(aftercond+1,modText.length());
//			System.out.println(id+":"+x+":"+y+":"+cond+":"+orient);
			this.add(x, y, id, cond, orient);
		}

	}

	/**
	 * Given a module ID, return that module if it is in the list.
	 * @param id The ID of the module to return.
	 * @return The module specified by the ID if it is in the list, null otherwise.
	 */
	public Module getByID(int id) {
		for(int i = 0; i < moduleList.size(); i++) {
			Module mod = moduleList.get(i);
			if (mod.getId() == id) {
				return mod;
			}
		}
		
		// module not found
		return null;
	}
	
	private ArrayList<Module> moduleList;
	private int counter[];
	public static boolean UP = true;
	public static boolean DOWN = false;
}
